package vn.student.mylib;

import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class ListClassTypeAdapter extends ArrayAdapter<ClassTypeInfo>{
	Activity act;
	public ArrayList<ClassTypeInfo> list;
	public ListClassTypeAdapter(Activity act,View view, ArrayList<ClassTypeInfo> list) {
		super(act, R.layout.class_type_row,list);
		this.act=act;
		this.list = list;
		if(list!=null&&list.size()>0){
			ListView tmp = (ListView)view.findViewById(R.id.classTypeListView);
			tmp.getLayoutParams().height=list.size()*50+10;
		}
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	     if (row==null) {                         
	    	 LayoutInflater inflater=act.getLayoutInflater();
	        
	    	 row=inflater.inflate(R.layout.class_type_row, null);
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     ClassTypeInfo classType = list.get(position);
	     wrapper.getClassTypeNameLabel().setText(classType.classTypeName);
	     if(list.size()==1){
	    	 row.setBackgroundResource(R.drawable.class_row_all);
	   	 }
	   	 else{
	   		 if(position==(list.size()-1))
	   			 row.setBackgroundResource(R.drawable.class_row_bottom);
	   		 else 
	   			 if(position==0)
	   				 row.setBackgroundResource(R.drawable.class_row_top);
	   			 else
	   				row.setBackgroundResource(R.drawable.class_row_none);
	   	 }
	     return(row);
	}
	private class RowWrapper {
		View base;
		TextView typeName = null;
	  
		RowWrapper(View base) {
			this.base=base;
		}
		TextView getClassTypeNameLabel() {
			if (typeName==null) {
				typeName=(TextView)base.findViewById(R.id.typeName);
			}
		  return(typeName);
		}
	}
}

